package com.ftg.learn.chapter06;

import java.util.Arrays;
import java.util.Queue;

/**
 * 数据结构队列
 *
 * @author lijiaxin
 * @version 1.0
 * @date 2023/7/14 9:13
 */

public class Array {

       private int[] queueArray;
       private int index=0;
       private  int default_capacity=10;
       private  int max_len=Integer.MAX_VALUE;

       public Array(){
           queueArray=new int[default_capacity];
       }

       public boolean append(int x){
           long newCapacity = queueArray.length + (queueArray.length >> 1);
           int lastCapacity = newCapacity > max_len ? max_len : (int) newCapacity;
           if (index == queueArray.length) {
               queueArray = Arrays.copyOf(queueArray, lastCapacity);
           }
           queueArray[index++]=x;
           return true;
       }
        public int getHeader(){
           //容错 当index=0 没有了
            //jdk提供的拷贝方法
            int t=queueArray[0];
            if(index!=1){
                System.arraycopy(queueArray,1,queueArray,0,index-1);
            }
            index--;
           return t;
        }

    public static void main(String[] args) {
        Array q=new Array();
        q.append(1);
        q.append(2);

        System.out.println(q.getHeader());
        System.out.println(q.getHeader());
    }

}
